package com.jinyi.wwdeng.ui.login

import android.graphics.Typeface
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.core.content.ContextCompat
import com.jinyi.base.mvp.BaseMvpActivity
import com.jinyi.base.utils.BarUtils
import com.jinyi.base.utils.thread.TimingThread
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActLoginBinding
import com.jinyi.wwdeng.ui.WelcomeAct
import com.jinyi.wwdeng.ui.login.contract.LoginContract
import com.jinyi.wwdeng.ui.login.presenter.LoginPresenter
import com.jinyi.wwdeng.utils.dialog.LoginDigitalCodeDialog

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/15 15:11
 * @Annotation：注册/登录
 */
class LoginAct: BaseMvpActivity<LoginContract.View, LoginPresenter>(), LoginContract.View,TimingThread.ITimingThreadListener{

    lateinit var binding: ActLoginBinding
    //验证码Dialog
    var mDialog: LoginDigitalCodeDialog? = null
    //倒计时
    var count = 60
    private var timeThread: TimingThread? = null

    override fun getRootView(): View {
        binding = ActLoginBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.GONE)
        initPresenter()

        binding.parentLayout.setPadding(0,BarUtils.getStatusBarHeight(),0,0)
        checkNextBtnState()
    }

    override fun initListener() {
        //客服
        binding.ivKefu.setOnClickListener {
            showToast("客服")
        }

        //电话号码入框监听
        binding.editNumber.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable?) {
                checkNextBtnState()
                if (getEdittextString().isNotEmpty()){
                    binding.editNumber.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD))
                } else {
                    binding.editNumber.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.NORMAL))
                }
            }
        })

        //隐私政策切换
        binding.checkBox.setOnCheckedChangeListener { _, _ -> checkNextBtnState() }

        //Next
        binding.btnNext.setOnClickListener {
            showDigitalDialog()
        }
    }

    override fun initData() {
//        presenter.requestData("")
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun showSuccessData(data: String?) {
    }

    override fun showFailureData(data: String?) {
    }

    /**
     * 设置按钮状态
     */
    private fun checkNextBtnState(){
        if (binding.checkBox.isChecked && getEdittextString().isNotEmpty()){
            binding.btnNext.setTextColor(ContextCompat.getColor(this, R.color.color_021e00))
            binding.btnNext.setBackgroundResource(R.drawable.base_circle_fill_9fe76f)
            binding.btnNext.isEnabled = true
        } else {
            binding.btnNext.setTextColor(ContextCompat.getColor(this, com.jinyi.lib_base.R.color.color_ffffff))
            binding.btnNext.setBackgroundResource(R.drawable.base_circle_fill_d0d4dd)
            binding.btnNext.isEnabled = false
        }
    }

    /**
     * 获取输入框内容
     */
    private fun getEdittextString(): String{
        return binding.editNumber.text.toString().trim()
    }

    /**
     * 验证码输入框
     */
    private fun showDigitalDialog(){
        if (mDialog == null){
            mDialog = LoginDigitalCodeDialog(this,object : LoginDigitalCodeDialog.IDialogCallback{
                //点击操作监听
                override fun onClick(view: View?) {
                    when(view!!.id){
                        R.id.ivClose -> {//关闭
                            if (timeThread != null){
                                timeThread!!.stopThread()
                            }
                            mDialog!!.dismiss()
                        }
                        R.id.resendLayout -> {//重发

                        }
                        R.id.ivRefresh -> {//刷新

                        }
                    }
                }
                //输入完成监听
                override fun inputComplete(content: String) {
                    when(content){
                        "1234" -> {
                            onHideSoftInput(mDialog!!.binding.editCode)
                            mDialog!!.dismiss()
                            onIntent(WelcomeAct::class.java)
                            finish()
                        }
                        "0000" -> {
                            //显示验证码错误样式
                            mDialog!!.showWrongLayout()
                        }
                        else -> {
                            mDialog!!.showCountdownLayout(count)
                        }
                    }
                }
            })
            mDialog!!.setCancelable(false)
            mDialog!!.setCanceledOnTouchOutside(false)
        }
        mDialog!!.show()
        startTimeThread()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread(){
        if (timeThread != null){
            timeThread!!.stopThread()
        }
        count = 60
        mDialog!!.showCountdownLayout(count)
        if (timeThread == null){
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    //倒计时
    override fun onLoading() {
        if (count <= 1){
            count = 60
            timeThread!!.stopThread()
            mDialog!!.showRefreshLayout()
            return
        }
        count--
        mDialog!!.onRefreshCountdown(count)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (timeThread != null){
            timeThread!!.stopThread()
            timeThread = null
        }
        if (mDialog != null){
            mDialog = null
        }
    }
}